package com.video;

import android.content.Context;
import android.content.ContextWrapper;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.WindowManager;

public class VideoActivity extends AppCompatActivity {
    JCVideoPlayerStandard mPLayer;
    private String mTitle, mVideoUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //隐藏状态栏
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        setContentView(R.layout.activity_video);
        mPLayer = (JCVideoPlayerStandard) findViewById(R.id.jc_player);
        ViewCompat.setTransitionName(mPLayer, "anima");
        mTitle = getIntent().getStringExtra("title");
        mVideoUrl = getIntent().getStringExtra("videoUrl");
        mPLayer.setUp(mVideoUrl, JCVideoPlayerStandard.SCREEN_LAYOUT_NORMAL, mTitle);
        mPLayer.startVideo();
        mPLayer.startWindowFullscreen();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mPLayer.startVideo();
        mPLayer.startWindowFullscreen();

    }

    @Override
    protected void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 重写attachBaseContext解决内存泄漏
     */
    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(new ContextWrapper(newBase) {
            @Override
            public Object getSystemService(String name) {
                if (Context.AUDIO_SERVICE.equals(name))
                    return getApplicationContext().getSystemService(name);
                return super.getSystemService(name);
            }
        });
    }


}